﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace TaxSetup
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private SalesManagerEntities _context = new SalesManagerEntities();
        private CollectionViewSource _taxClassesViewSource;
        private CollectionViewSource _taxZonesViewSource;
        private CollectionViewSource _taxRatesViewSource;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            _taxClassesViewSource = (CollectionViewSource)Resources["TaxClasses"];
            _taxZonesViewSource = (CollectionViewSource)Resources["TaxZones"];
            _taxRatesViewSource = (CollectionViewSource)Resources["TaxRates"];
            _taxClassesViewSource.Source = _context.tblTaxClasses;
            _taxZonesViewSource.Source = _context.tblTaxZones;
        }

        private void btnAddNewTaxClass_Click(object sender, RoutedEventArgs e)
        {
            tblTaxClass newTaxClass = (tblTaxClass)((IEditableCollectionView)_taxClassesViewSource.View).AddNew();
            ((IEditableCollectionView)_taxClassesViewSource.View).CommitNew();
            newTaxClass.Name = "New";
            _context.AddTotblTaxClasses(newTaxClass);
            _taxClassesViewSource.View.MoveCurrentTo(newTaxClass);
        }

        private void btnCopyTaxClass_Click(object sender, RoutedEventArgs e)
        {
        }

        private void btnDeleteTaxClass_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnRemoveZone_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnAddClassZone_Click(object sender, RoutedEventArgs e)
        {
            DialogTaxZoneList dlg = new DialogTaxZoneList(_context);
            if ((bool)dlg.ShowDialog())
            {
                //tblSequence newSequence = new tblSequence();
                tblTaxRate newTaxRate = (tblTaxRate)((IEditableCollectionView)_taxRatesViewSource.View).AddNew();
                ((IEditableCollectionView)_taxRatesViewSource.View).CommitNew();
                newTaxRate.Name = "new";
                newTaxRate.Rate = 0;
                newTaxRate.TaxShipping = true;
                newTaxRate.TaxZone = dlg.TaxZone;
                newTaxRate.TaxClass = (tblTaxClass)_taxClassesViewSource.View.CurrentItem;
                _taxRatesViewSource.View.MoveCurrentTo(newTaxRate);
            }
        }

        private void newZone_Click(object sender, RoutedEventArgs e)
        {
            DialogNewName dlg = new DialogNewName();
            dlg.Title = "New Tax Zone";
            if ((bool)dlg.ShowDialog())
            {
                tblTaxZone newTaxZone = (tblTaxZone)((IEditableCollectionView)_taxZonesViewSource.View).AddNew();
                ((IEditableCollectionView)_taxZonesViewSource.View).CommitNew();
                newTaxZone.Name = dlg.EnteredName;
                _taxZonesViewSource.View.MoveCurrentTo(newTaxZone);
            }
        }
    }
}
